<?php

namespace App\Admin\Forms;

use App\Helpers\Lib\WechatMini;
use App\Models\TongDistrict;
use App\Models\TongOrder;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;

class SendOrderForm extends Form
{
    use LazyWidget;

    /**
     * Handle the form request.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {
        TongOrder::query()->where('id',$input['id'])->update([
            'express_num'=>$input['express_num'],
            'express_company'=>$input['express_company'],
            'send_time'=>date('Y-m-d h:i:s',time()),
            'status'=>4,//已发货，待收货
        ]);

        // return $this->response()->error('Your error message.');
        return $this
				->response()
				->success('订单发货成功')
				->refresh();
    }

    /**
     * Build a form here.
     */
    public function form()
    {
        //$id = $this->payload['id'] ?? null;
        $this->hidden('id','id');
        $this->text('express_num','快递单号')->required();
        $new_res = [];
        if(config('app.is_local')==false){
            $res = WechatMini::getDelivery();
            foreach ($res as $k=>$v){
                $new_res[$v['delivery_id']] = $v['delivery_name'];

            }
        }

        $this->select('express_company','物流公司')->options($new_res)->required();
    }

    /**
     * The data of the form.
     *
     * @return array
     */
    public function default()
    {
        return [
            'id'=>$this->payload['id'] ?? null,
            'express_num'  => '',
            'express_company' => '',
        ];
    }
}
